// ----------------------------------
// RSDK Project: Sonic 2
// Script Description: Stegway Object
// Script Author: Christian Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

// ========================
// Aliases
// ========================

private alias object.value0 : object.timer
private alias object.value1 : object.startPos.x
private alias object.value2 : object.startPos.y
private alias object.value3 : object.showJet
private alias object.value4 : object.jetAnimTimer
private alias object.value5 : object.wheelFrame
private alias object.value6 : object.wheelAnimTimer
private alias object.value7 : object.noFloor

// States
private alias 0 : STEGWAY_MOVING
private alias 1 : STEGWAY_TURN
private alias 2 : STEGWAY_REVUP
private alias 3 : STEGWAY_REVRELEASE
private alias 4 : STEGWAY_DASH

// Animations
private alias 0 : STEGWAY_ANI_MOVE
private alias 1 : STEGWAY_ANI_REVRELEASE
private alias 2 : STEGWAY_ANI_REVUP
private alias 3 : STEGWAY_ANI_DASH

// Player Aliases
private alias object.value40 : player.hitboxLeft
private alias object.value38 : player.hitboxTop
private alias object.value41 : player.hitboxRight
private alias object.value39 : player.hitboxBottom

// Path ID Aliases
private alias 0 : PATH_A


// ========================
// Function Declarations
// ========================

reserve function Stegway_DebugDraw
reserve function Stegway_DebugSpawn


// ========================
// Function Definitions
// ========================

private function Stegway_DebugDraw
	DrawSpriteFX(0, FX_FLIP, object.xpos, object.ypos)
end function


private function Stegway_DebugSpawn
	CreateTempObject(TypeName[Stegway], 0, object.xpos, object.ypos)
	if object.direction == FACING_RIGHT
		object[tempObjectPos].direction = FLIP_NONE
		object[tempObjectPos].xvel = -0x4000
	else
		object[tempObjectPos].direction = FLIP_X
		object[tempObjectPos].xvel = 0x4000
	end if
end function


// ========================
// Events
// ========================

event ObjectUpdate
	switch object.state
	case STEGWAY_MOVING
		if object.priority != PRIORITY_XBOUNDS_DESTROY
			object.priority = PRIORITY_ACTIVE
		end if

		object.xpos += object.xvel
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			if object.direction == FLIP_NONE
				BoxCollisionTest(C_TOUCH, object.entityPos, -96, -32, 0, 14, currentPlayer, 0, 0, 0, 0)
			else
				BoxCollisionTest(C_TOUCH, object.entityPos, 0, -32, 96, 14, currentPlayer, 0, 0, 0, 0)
			end if

			if checkResult == true
				object.state = STEGWAY_REVUP
				object.animation = STEGWAY_ANI_DASH
			end if
		next

		if object.direction == FLIP_NONE
			ObjectTileGrip(CSIDE_FLOOR, -12, 16, PATH_A)
		else
			ObjectTileGrip(CSIDE_FLOOR, 12, 16, PATH_A)
		end if

		if checkResult == false
			object.state = STEGWAY_TURN
			object.animation = STEGWAY_ANI_DASH
			object.showJet = false
			if object.frame == 3
				object.frame = 2
			end if
		end if
		break

	case STEGWAY_TURN
		if object.timer < 29
			object.timer++
		else
			object.timer = 0
			object.state = STEGWAY_MOVING
			object.animation = STEGWAY_ANI_MOVE
			object.direction ^= FLIP_X
			if object.direction == FLIP_NONE
				object.xvel = -0x4000
			else
				object.xvel = 0x4000
			end if
		end if
		break

	case STEGWAY_REVUP
		object.timer++
		if object.timer == 8
			object.timer = 0
			object.state = STEGWAY_REVRELEASE
			object.animation = STEGWAY_ANI_REVUP
			PlaySfx(SfxName[Rev Up], false)
		end if
		break

	case STEGWAY_REVRELEASE
		object.timer++
		if object.timer == 32
			object.timer = 0
			object.state = STEGWAY_DASH
			object.animation = STEGWAY_ANI_REVRELEASE
			object.showJet = true
			object.xvel *= 12
			PlaySfx(SfxName[Release], false)
		end if
		break

	case STEGWAY_DASH
		object.xpos += object.xvel

		if object.direction == FLIP_NONE
			if object.noFloor == false
				temp0 = object.ypos
				ObjectTileGrip(CSIDE_FLOOR, -64, 16, PATH_A)
				if checkResult == false
					object.noFloor = true
				end if

				object.ypos = temp0
			end if

			ObjectTileGrip(CSIDE_FLOOR, -12, 16, PATH_A)
		else
			if object.noFloor == false
				temp0 = object.ypos
				ObjectTileGrip(0, 64, 16, PATH_A)
				if checkResult == false
					object.noFloor = true
				end if

				object.ypos = temp0
			end if

			ObjectTileGrip(CSIDE_FLOOR, 12, 16, PATH_A)
		end if

		if object.noFloor == true
			if ringTimer == 0
				CreateTempObject(TypeName[Dust Puff], 0, object.xpos, object.ypos)
				if object.direction == FLIP_NONE
					object[tempObjectPos].ixpos += 10
				else
					object[tempObjectPos].ixpos -= 10
				end if
				object[tempObjectPos].iypos += 16
			end if

			if object.xvel >= 0x4000
				object.xvel -= 0xA00
				if object.xvel < 0x4000
					object.xvel = 0x4000
					object.state = STEGWAY_MOVING
					object.animation = STEGWAY_ANI_MOVE
					object.animationTimer = 0
					object.frame = 1
					object.showJet = false
					object.noFloor = false
				end if
			end if

			if object.xvel <= -0x4000
				object.xvel += 0xA00
				if object.xvel > -0x4000
					object.xvel = -0x4000
					object.state = STEGWAY_MOVING
					object.animation = STEGWAY_ANI_MOVE
					object.animationTimer = 0
					object.frame = 1
					object.showJet = false
					object.noFloor = false
				end if
			end if
		end if

		if checkResult == false
			object.state = STEGWAY_TURN
			object.animation = STEGWAY_ANI_DASH
			object.showJet = false
			object.noFloor = false
			if object.frame == 3
				object.frame = 2
			end if
		end if
		break
		
	end switch

	if object.outOfBounds == true
		temp0 = object.xpos
		temp1 = object.ypos
		object.xpos = object.startPos.x
		object.ypos = object.startPos.y
		if object.outOfBounds == true
			if object.propertyValue == 0
				object.direction = FLIP_NONE
				object.xvel = -0x4000
			else
				object.direction = FLIP_X
				object.xvel = 0x4000
			end if

			object.animation = STEGWAY_ANI_MOVE
			if object.priority != PRIORITY_XBOUNDS_DESTROY
				object.priority = PRIORITY_BOUNDS
			end if

			object.showJet = false
			object.wheelFrame = 0
			object.jetAnimTimer = 0
			object.noFloor = false
			object.state = STEGWAY_MOVING
		else
			object.xpos = temp0
			object.ypos = temp1
		end if
	end if

	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		BoxCollisionTest(C_TOUCH, object.entityPos, -20, -14, 20, 14, currentPlayer, player[currentPlayer].hitboxLeft, player[currentPlayer].hitboxTop, player[currentPlayer].hitboxRight, player[currentPlayer].hitboxBottom)
		if checkResult == true
			CallFunction(Player_BadnikBreak)
		end if
	next

	switch object.animation
	case STEGWAY_ANI_MOVE
		if object.animationTimer == 0
			object.frame++
			object.frame %= 3
			switch object.frame
			case 0
				object.animationTimer = 14
				break
				
			case 1
				object.animationTimer = 6
				break
				
			case 2
				object.animationTimer = 10
				break
			end switch
		else
			object.animationTimer--
		end if

		object.wheelAnimTimer++
		object.wheelAnimTimer &= 15
		object.wheelFrame = object.wheelAnimTimer
		object.wheelFrame >>= 3
		break

	case STEGWAY_ANI_REVRELEASE
		object.frame = 3
		object.wheelAnimTimer++
		object.wheelAnimTimer %= 6
		if object.noFloor == true
			object.wheelFrame = 0
		else
			object.wheelFrame = object.wheelAnimTimer
			object.wheelFrame /= 3
		end if
		break

	case STEGWAY_ANI_REVUP
		object.frame = 2
		object.wheelAnimTimer++
		object.wheelAnimTimer &= 3
		object.wheelFrame = object.wheelAnimTimer
		object.wheelFrame >>= 1
		break

	case STEGWAY_ANI_DASH
		break
		
	end switch

	if object.showJet == true
		object.jetAnimTimer++
		object.jetAnimTimer &= 3
	end if
end event


event ObjectDraw
	DrawSpriteFX(object.frame, FX_FLIP, object.xpos, object.ypos)

	if object.wheelFrame == 1
		DrawSpriteFX(4, FX_FLIP, object.xpos, object.ypos)
	end if

	if object.showJet == true
		temp0 = object.jetAnimTimer
		temp0 >>= 1
		temp0 += 5
		DrawSpriteFX(temp0, FX_FLIP, object.xpos, object.ypos)
	end if
end event


event ObjectStartup
	LoadSpriteSheet("HPZ/Objects.gif")
	SpriteFrame(-24, -16, 48, 32, 40, 57) 	// moving 1 - #0
	SpriteFrame(-24, -16, 48, 32, 89, 57) 	// moving 2 - #1
	SpriteFrame(-24, -16, 48, 32, 82, 90) 	// dashing 1 - #2
	SpriteFrame(-24, -16, 48, 32, 33, 90) 	// dashing 2 - #3
	SpriteFrame(0, 0, 24, 16, 198, 29) 		// wheel - #4
	SpriteFrame(24, 4, 7, 6, 206, 7) 		// jet 1 - #5
	SpriteFrame(24, 4, 8, 6, 206, 14)		// jet 2 - #6

	foreach (TypeName[Stegway], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].startPos.x = object[arrayPos0].xpos
		object[arrayPos0].startPos.y = object[arrayPos0].ypos
		if object[arrayPos0].propertyValue == 0
			object[arrayPos0].direction = FLIP_NONE
			object[arrayPos0].xvel = -0x4000
		else
			object[arrayPos0].direction = FLIP_X
			object[arrayPos0].xvel = 0x4000
		end if
	next

	SetTableValue(TypeName[Stegway], DebugMode_ObjCount, DebugMode_TypesTable)
	SetTableValue(Stegway_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
	SetTableValue(Stegway_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
	DebugMode_ObjCount++
end event


// ========================
// Editor Events
// ========================

event RSDKEdit
	if editor.returnVariable == true
		switch editor.variableID
		case EDIT_VAR_PROPVAL // property value
			checkResult = object.propertyValue
			checkResult &= 1
			break
			
		case 0 // direction
			GetBit(checkResult, object.propertyValue, 0)
			break
			
		end switch
	else
		switch editor.variableID
		case EDIT_VAR_PROPVAL // property value
			object.propertyValue = editor.variableValue
			object.propertyValue &= 1
			break
			
		case 0 // direction
			CheckNotEqual(editor.variableValue, 0)
			SetBit(object.propertyValue, 0, checkResult)
			break
			
		end switch
	end if
end event


event RSDKDraw
	GetBit(object.direction, object.propertyValue, 0)
	DrawSpriteFX(0, FX_FLIP, object.xpos, object.ypos)
end event


event RSDKLoad
	LoadSpriteSheet("HPZ/Objects.gif")
	SpriteFrame(-24, -16, 48, 32, 40, 57)

	AddEditorVariable("direction")
	SetActiveVariable("direction")
	AddEnumVariable("Left", 0)
	AddEnumVariable("Right", 1)
end event
